<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.5.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="图形显示方法：图素法-以图形对象为基本元素（矢量图）图像法-以像素点为基本单位（标量图）">
<meta name="keywords" content="计算机,语言,python,交互式图形编程">
<meta property="og:type" content="article">
<meta property="og:title" content="图形编程">
<meta property="og:url" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;2019&#x2F;12&#x2F;18&#x2F;%E5%9B%BE%E5%BD%A2%E7%BC%96%E7%A8%8B&#x2F;index.html">
<meta property="og:site_name" content="chasemyking">
<meta property="og:description" content="图形显示方法：图素法-以图形对象为基本元素（矢量图）图像法-以像素点为基本单位（标量图）">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;图像简单实例.png">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;鼠标点击输出作标.png">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;点击鼠标画五边形.png">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;celsius-converter.png">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;画布移动.png">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;GraphWin对象常用方法.png">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;图形对象通用方法.png">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;Point对象方法.png">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;Line对象方法.png">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;Circle对象方法.png">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;Rectangle对象方法.png">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;Oval对象方法.png">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;Polyon对象方法.png">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;Text对象方法.png">
<meta property="og:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;Celsius-coverte-change-color.png">
<meta property="og:updated_time" content="2019-12-22T03:31:48.165Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https:&#x2F;&#x2F;chasemyking.github.io&#x2F;images&#x2F;图像简单实例.png">

<link rel="canonical" href="https://chasemyking.github.io/2019/12/18/%E5%9B%BE%E5%BD%A2%E7%BC%96%E7%A8%8B/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>图形编程 | chasemyking</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?0e2a4e5bd5a98fcd51a1d8d5405bdfb8";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">chasemyking</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://chasemyking.github.io/2019/12/18/%E5%9B%BE%E5%BD%A2%E7%BC%96%E7%A8%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/chasemyking.jpg">
      <meta itemprop="name" content="chasemyking">
      <meta itemprop="description" content="我永远喜欢艾斯德斯！">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="chasemyking">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          图形编程
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-18 21:37:01" itemprop="dateCreated datePublished" datetime="2019-12-18T21:37:01+08:00">2019-12-18</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-12-22 11:31:48" itemprop="dateModified" datetime="2019-12-22T11:31:48+08:00">2019-12-22</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%AD%A6%E4%B9%A0/" itemprop="url" rel="index">
                    <span itemprop="name">学习</span>
                  </a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%AD%A6%E4%B9%A0/%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index">
                    <span itemprop="name">笔记</span>
                  </a>
                </span>
            </span>

          
            <span id="/2019/12/18/%E5%9B%BE%E5%BD%A2%E7%BC%96%E7%A8%8B/" class="post-meta-item leancloud_visitors" data-flag-title="图形编程" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>4.9k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>4 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>图形显示方法：<br>图素法-以图形对象为基本元素（矢量图）<br>图像法-以像素点为基本单位（标量图）  </p>
<a id="more"></a>
<p>python图形工具包：<br>    tkinter ：python标准GUI<br>    Graphics：基于tkinter扩展<br>    turtle:python内置的图形库<br>图形用户界面（GUI）</p>
<p>简单的实例：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> graphics <span class="keyword">import</span> *<span class="comment">#导入模块</span></span><br><span class="line">p1=Point(<span class="number">100</span>,<span class="number">100</span>)</span><br><span class="line">p2=Point(<span class="number">150</span>,<span class="number">80</span>)</span><br><span class="line">win=GraphWin()<span class="comment">#创建图形窗</span></span><br><span class="line">p1.draw(win)<span class="comment">#画点</span></span><br><span class="line">p2.draw(win)</span><br></pre></td></tr></table></figure>
<p>如图：<br><img src="/images/图像简单实例.png" width="25%" class="full-image"/><br>图形对象的概念：<br>用面向对象的思想来理解。<br>类的实例：<br>定义了实例包含的属性和方法。<br>比如point（）中的p1和p2<br>改变对象实例变量的属性值改变对象的状态：<br>move（x，y）将对象沿x，y的方向分别移动x，y。<br>Circle（point，x） 以point为圆心，x为半径划圆。</p>
<p>图形用户界面（图形用户接口）<br>1.图形方式计算机操作界面。<br>2.用于程序的输入和输出。<br>3.事件驱动。<br>简单例子：<br>(1)用户点击10次鼠标返回做标值  </p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> graphics <span class="keyword">import</span>*<span class="comment">#引用模块</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    win=GraphWin(<span class="string">"click me!"</span>)<span class="comment">#定义图形窗口</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">10</span>):</span><br><span class="line">        p=win.getMouse()<span class="comment">#获得点击作标</span></span><br><span class="line">        p.draw(win)<span class="comment">#将这个点表示出来</span></span><br><span class="line">        print(<span class="string">"you clicked at:"</span>,p.getX(),p.getY())</span><br><span class="line"><span class="keyword">if</span> __name__==<span class="string">"__main__"</span>:</span><br><span class="line">    main()</span><br></pre></td></tr></table></figure>
<p>结果如下：<br><img src="/images/鼠标点击输出作标.png" width="50%"></p>
<p>(2)绘制一个五边形  </p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> graphics <span class="keyword">import</span> *</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    win=GraphWin(<span class="string">"Draw a polgon"</span>,<span class="number">300</span>,<span class="number">300</span>)</span><br><span class="line">    win.setCoords(<span class="number">0.0</span>,<span class="number">0.0</span>,<span class="number">300.0</span>,<span class="number">300.0</span>)</span><br><span class="line">    message=Text(Point(<span class="number">150</span>,<span class="number">20</span>),<span class="string">"click on five points"</span>)<span class="comment">#输出提示信息</span></span><br><span class="line">    message.draw(win)</span><br><span class="line">    p1=win.getMouse()</span><br><span class="line">    p1.draw(win)</span><br><span class="line">    p2=win.getMouse()</span><br><span class="line">    p2.draw(win)</span><br><span class="line">    p3=win.getMouse()</span><br><span class="line">    p3.draw(win)</span><br><span class="line">    p4=win.getMouse()</span><br><span class="line">    p4.draw(win)</span><br><span class="line">    p5=win.getMouse()</span><br><span class="line">    p5.draw(win)</span><br><span class="line">    polygon=Polygon(p1,p2,p3,p4,p5)<span class="comment">#Polygon多边形绘制函数（连线顺序）</span></span><br><span class="line">    polygon.setFill(<span class="string">"peachpuff"</span>)<span class="comment">#填充颜色</span></span><br><span class="line">    polygon.setOutline(<span class="string">"black"</span>)<span class="comment">#线条颜色</span></span><br><span class="line">    polygon.draw(win)</span><br><span class="line">    message.setText(<span class="string">"click anywhere to quit!"</span>)</span><br><span class="line">    win.getMouse()<span class="comment">#等待鼠标响应</span></span><br><span class="line">    win.close()<span class="comment">#关闭窗口</span></span><br><span class="line"><span class="keyword">if</span> __name__==<span class="string">"__main__"</span>:</span><br><span class="line">    main()</span><br></pre></td></tr></table></figure>
<p>结果如下：<br><img src="/images/点击鼠标画五边形.png" width="25%"></p>
<p>实例三：温度转化  </p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> graphics <span class="keyword">import</span> *</span><br><span class="line"> </span><br><span class="line">win = GraphWin(<span class="string">"Celsius Converter"</span>, <span class="number">400</span>, <span class="number">300</span>)</span><br><span class="line">win.setCoords(<span class="number">0.0</span>, <span class="number">0.0</span>, <span class="number">3.0</span>, <span class="number">4.0</span>)</span><br><span class="line"><span class="comment"># 绘制接口</span></span><br><span class="line">Text(Point(<span class="number">1</span>,<span class="number">3</span>), <span class="string">" Celsius Temperature:"</span>).draw(win)</span><br><span class="line">Text(Point(<span class="number">1</span>,<span class="number">1</span>), <span class="string">"Fahrenheit Temperature:"</span>).draw(win)</span><br><span class="line">input = Entry(Point(<span class="number">2</span>,<span class="number">3</span>), <span class="number">5</span>)</span><br><span class="line">input.setText(<span class="string">"0.0"</span>)</span><br><span class="line">input.draw(win)</span><br><span class="line">output = Text(Point(<span class="number">2</span>,<span class="number">1</span>),<span class="string">""</span>)</span><br><span class="line">output.draw(win)</span><br><span class="line">button = Text(Point(<span class="number">1.5</span>,<span class="number">2.0</span>),<span class="string">"Convert It"</span>)</span><br><span class="line">button.draw(win)</span><br><span class="line">Rectangle(Point(<span class="number">1</span>,<span class="number">1.5</span>), Point(<span class="number">2</span>,<span class="number">2.5</span>)).draw(win)</span><br><span class="line"><span class="comment"># 等待鼠标点击</span></span><br><span class="line">win.getMouse()</span><br><span class="line"><span class="comment"># 转换输入</span></span><br><span class="line">celsius = eval(input.getText())</span><br><span class="line">fahrenheit = <span class="number">9.0</span>/<span class="number">5.0</span> * celsius + <span class="number">32.0</span></span><br><span class="line"><span class="comment"># 显示输出，改变按钮</span></span><br><span class="line">output.setText(fahrenheit)</span><br><span class="line">button.setText(<span class="string">"Quit"</span>)</span><br><span class="line"><span class="comment"># 等待响应鼠标点击，退出程序</span></span><br><span class="line">win.getMouse()</span><br><span class="line">win.close()</span><br></pre></td></tr></table></figure>
<p>结果如下：<br><img src="/images/celsius-converter.png" width="25%"></p>
<p>Tkinter创建GUI程序<br>Tkinter 组件<br>Tkinter的提供各种控件，如按钮，标签和文本框，一个GUI应用程序中使用。这些控件通常被称为控件或者部件。  </p>
<p>目前有15种Tkinter的部件。我们提出这些部件以及一个简短的介绍，在下面的表:  </p>
<table>
<thead>
<tr>
<th>控件</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>Button</td>
<td>按钮控件；在程序中显示按钮。</td>
</tr>
<tr>
<td>Canvas</td>
<td>画布控件；显示图形元素如线条或文本</td>
</tr>
<tr>
<td>Checkbutton</td>
<td>多选框控件；用于在程序中提供多项选择框</td>
</tr>
<tr>
<td>Entry</td>
<td>输入控件；用于显示简单的文本内容</td>
</tr>
<tr>
<td>Frame</td>
<td>框架控件；在屏幕上显示一个矩形区域，多用来作为容器</td>
</tr>
<tr>
<td>Label</td>
<td>标签控件；可以显示文本和位图</td>
</tr>
<tr>
<td>Listbox</td>
<td>列表框控件；在Listbox窗口小部件是用来显示一个字符串列表给用户</td>
</tr>
<tr>
<td>Menubutton</td>
<td>菜单按钮控件，用于显示菜单项。</td>
</tr>
<tr>
<td>Menu</td>
<td>菜单控件；显示菜单栏,下拉菜单和弹出菜单</td>
</tr>
<tr>
<td>Message</td>
<td>消息控件；用来显示多行文本，与label比较类似</td>
</tr>
<tr>
<td>Radiobutton</td>
<td>单选按钮控件；显示一个单选的按钮状态</td>
</tr>
<tr>
<td>Scale</td>
<td>范围控件；显示一个数值刻度，为输出限定范围的数字区间</td>
</tr>
<tr>
<td>Scrollbar</td>
<td>滚动条控件，当内容超过可视化区域时使用，如列表框。</td>
</tr>
<tr>
<td>Text</td>
<td>文本控件；用于显示多行文本</td>
</tr>
<tr>
<td>Toplevel</td>
<td>容器控件；用来提供一个单独的对话框，和Frame比较类似</td>
</tr>
<tr>
<td>Spinbox</td>
<td>输入控件；与Entry类似，但是可以指定输入范围值</td>
</tr>
<tr>
<td>PanedWindow</td>
<td>PanedWindow是一个窗口布局管理的插件，可以包含一个或者多个子控件。</td>
</tr>
<tr>
<td>LabelFrame</td>
<td>labelframe 是一个简单的容器控件。常用与复杂的窗口布局。</td>
</tr>
<tr>
<td>tkMessageBox</td>
<td>用于显示你应用程序的消息框。</td>
</tr>
</tbody></table>
<p>标准属性：<br>标准属性也就是所有控件的共同属性，如大小，字体和颜色等等。  </p>
<table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>Dimension</td>
<td>控件大小；</td>
</tr>
<tr>
<td>Color</td>
<td>控件颜色；</td>
</tr>
<tr>
<td>Font</td>
<td>控件字体；</td>
</tr>
<tr>
<td>Anchor</td>
<td>锚点；</td>
</tr>
<tr>
<td>Relief</td>
<td>控件样式；</td>
</tr>
<tr>
<td>Bitmap</td>
<td>位图；</td>
</tr>
<tr>
<td>Cursor</td>
<td>光标；</td>
</tr>
</tbody></table>
<p>几何管理:<br>Tkinter控件有特定的几何状态管理方法，管理整个控件区域组织，一下是Tkinter公开的几何管理类：包、网格、位置  </p>
<table>
<thead>
<tr>
<th>几何方法</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>pack()</td>
<td>包装；</td>
</tr>
<tr>
<td>grid()</td>
<td>网格；</td>
</tr>
<tr>
<td>place()</td>
<td>位置；</td>
</tr>
</tbody></table>
<p>place(relx=?,rely=?,anchor=CENTER)<br>relx    指定组件的 X 坐标，以父容器总宽度为单位 1，该值应该在 0.0~1.0 之间，其中 0.0 代表位于窗口最左边，1.0 代表位于窗口最右边，0.5 代表位于窗口中间。<br>rely    指定组件的 Y 坐标，以父容器总高度为单位 1，该值应该在 0.0~1.0  之间，其中 0.0 代表位于窗口最上边，1.0 代表位于窗口最下边，0.5 代表位于窗口中间。</p>
<p>简单实例：<br>1.画布移动  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">from tkinter import *</span><br><span class="line"> </span><br><span class="line">def main():   </span><br><span class="line">    tk = Tk()</span><br><span class="line">    canvas = Canvas(tk, width = 400, height = 400)</span><br><span class="line">    canvas.pack()</span><br><span class="line"> </span><br><span class="line">    def moverectangle(event):</span><br><span class="line">        if event.keysym == &quot;Up&quot;:</span><br><span class="line">            canvas.move(1,0,-5)</span><br><span class="line">        elif event.keysym == &quot;Down&quot;:</span><br><span class="line">            canvas.move(1,0,5)</span><br><span class="line">        elif event.keysym == &quot;Left&quot;:</span><br><span class="line">            canvas.move(1,-5,0)</span><br><span class="line">        elif event.keysym == &quot;Right&quot;:</span><br><span class="line">            canvas.move(1,5,0)</span><br><span class="line">         </span><br><span class="line">    canvas.create_rectangle(180,180,220,220,fill=&quot;red&quot;)</span><br><span class="line">    canvas.bind_all(&quot;&lt;KeyPress-Up&gt;&quot;,moverectangle)</span><br><span class="line">    canvas.bind_all(&quot;&lt;KeyPress-Down&gt;&quot;,moverectangle)</span><br><span class="line">    canvas.bind_all(&quot;&lt;KeyPress-Left&gt;&quot;,moverectangle)</span><br><span class="line">    canvas.bind_all(&quot;&lt;KeyPress-Right&gt;&quot;,moverectangle)</span><br><span class="line"> </span><br><span class="line">if __name__ == &apos;__main__&apos;:</span><br><span class="line">    main()</span><br></pre></td></tr></table></figure>
<p>结果如图：<br><img src="/images/画布移动.png" width="25%"></p>
<p>图形库的应用方法：<br>GraphWin对象：<br>    程序可以定义任意数量的窗体<br>    GraphWin()<br>    默认标题是“GraphWin Windows”<br>    默认大小为200*200</p>
<p>常用方法：<br><img src="/images/GraphWin对象常用方法.png" width="100%"></p>
<p>几种常见的图形对象：<br>点，线段，圆，椭圆，矩形，多边形以及文本</p>
<p>图形对象通用方法：<br><img src="/images/图形对象通用方法.png" width="100%"></p>
<p>Point对象方法：<br><img src="/images/Point对象方法.png" width="100%"></p>
<p>Line对象方法：<br><img src="/images/Line对象方法.png" width="100%"></p>
<p>Circle对象方法：<br><img src="/images/Circle对象方法.png" width="100%"></p>
<p>Rectangle对象方法：<br><img src="/images/Rectangle对象方法.png" width="100%"></p>
<p>Oval对象方法：<br><img src="/images/Oval对象方法.png" width="100%"></p>
<p>Polygon对象方法：<br><img src="/images/Polyon对象方法.png" width="100%"></p>
<p>Text对象方法：<br><img src="/images/Text对象方法.png" width="100%"></p>
<p>简单例子：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> graphics <span class="keyword">import</span> *</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">convert</span><span class="params">(inpu)</span>:</span></span><br><span class="line">    celsius = eval(inpu.getText())    <span class="comment"># 输入转换</span></span><br><span class="line">    fahrenheit = <span class="number">9.0</span>/<span class="number">5.0</span> * celsius + <span class="number">32</span></span><br><span class="line">    <span class="keyword">return</span> fahrenheit </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">colorChange</span><span class="params">(win,inpu)</span>:</span></span><br><span class="line">    cnum = eval(inpu.getText())</span><br><span class="line">    weight = int(cnum / <span class="number">100.0</span>)</span><br><span class="line">    newcolor =color_rgb(<span class="number">255</span>*weight,<span class="number">66</span>+<span class="number">150</span>*(<span class="number">1</span>-weight),<span class="number">255</span>*(<span class="number">1</span>-weight))</span><br><span class="line">    win.setBackground(newcolor)</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    win = GraphWin(<span class="string">"Celsius Converter"</span>, <span class="number">400</span>, <span class="number">300</span>)</span><br><span class="line">    win.setCoords(<span class="number">0.0</span>, <span class="number">0.0</span>, <span class="number">3.0</span>, <span class="number">4.0</span>)</span><br><span class="line">    <span class="comment"># 绘制输入接口</span></span><br><span class="line">    Text(Point(<span class="number">1</span>,<span class="number">3</span>),</span><br><span class="line">         <span class="string">" Celsius Temperature:"</span>).draw(win)</span><br><span class="line">    Text(Point(<span class="number">2</span>,<span class="number">2.7</span>),</span><br><span class="line">         <span class="string">" (Please input 0.0-100.0 )"</span>).draw(win)</span><br><span class="line">    Text(Point(<span class="number">1</span>,<span class="number">1</span>),</span><br><span class="line">         <span class="string">"Fahrenheit Temperature:"</span>).draw(win)</span><br><span class="line">    inpu = Entry(Point(<span class="number">2</span>,<span class="number">3</span>), <span class="number">5</span>)</span><br><span class="line">    inpu.setText(<span class="string">"0.0"</span>)</span><br><span class="line">    inpu.draw(win)</span><br><span class="line">    output = Text(Point(<span class="number">2</span>,<span class="number">1</span>),<span class="string">""</span>)</span><br><span class="line">    output.draw(win)</span><br><span class="line">    button = Text(Point(<span class="number">1.5</span>,<span class="number">2.0</span>),<span class="string">"Convert It"</span>)</span><br><span class="line">    button.draw(win)</span><br><span class="line">    rect = Rectangle(Point(<span class="number">1</span>,<span class="number">1.5</span>), Point(<span class="number">2</span>,<span class="number">2.5</span>))</span><br><span class="line">    rect.draw(win)</span><br><span class="line">    <span class="comment"># 等待鼠标点击</span></span><br><span class="line">    win.getMouse()</span><br><span class="line">    result = convert(inpu)    <span class="comment"># 转换输入</span></span><br><span class="line">    output.setText(result)    <span class="comment"># 显示输出 </span></span><br><span class="line">    <span class="comment"># 改变颜色</span></span><br><span class="line">    colorChange(win,inpu)</span><br><span class="line">    <span class="comment"># 改变按钮字体</span></span><br><span class="line">    button.setText(<span class="string">"Quit"</span>)</span><br><span class="line">    <span class="comment"># 等待点击事件，退出程序</span></span><br><span class="line">    win.getMouse()</span><br><span class="line">    win.close()</span><br><span class="line"> </span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    main()</span><br></pre></td></tr></table></figure>

<p>结果：<br><img src="/images/Celsius-coverte-change-color.png" width="50%"></p>

    </div>

    
    
    
      
        <div class="reward-container">
  <div>这么精彩！不赞一个吗！^_^</div>
  <button disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.png" alt="chasemyking 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.jpg" alt="chasemyking 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA/" rel="tag"># 计算机</a>
              <a href="/tags/%E8%AF%AD%E8%A8%80/" rel="tag"># 语言</a>
              <a href="/tags/python/" rel="tag"># python</a>
              <a href="/tags/%E4%BA%A4%E4%BA%92%E5%BC%8F%E5%9B%BE%E5%BD%A2%E7%BC%96%E7%A8%8B/" rel="tag"># 交互式图形编程</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
                <a href="/2019/12/13/%E5%AD%97%E5%85%B8/" rel="next" title="字典">
                  <i class="fa fa-chevron-left"></i> 字典
                </a>
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
                <a href="/2019/12/21/%E7%A8%8B%E5%BA%8F%E6%89%A9%E5%85%85/" rel="prev" title="程序扩充">
                  程序扩充 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <img class="site-author-image" itemprop="image" alt="chasemyking"
    src="/images/chasemyking.jpg">
  <p class="site-author-name" itemprop="name">chasemyking</p>
  <div class="site-description" itemprop="description">我永远喜欢艾斯德斯！</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">16</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/chasemyking/chasemyking.github.io" title="GitHub &amp;rarr; https:&#x2F;&#x2F;github.com&#x2F;chasemyking&#x2F;chasemyking.github.io" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/mailto:cangtiandadi2016@outlook.com" title="E-Mail &amp;rarr; mailto:cangtiandadi2016@outlook.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">chasemyking</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">59k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">54 分钟</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v4.0.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.5.0
  </div>

        






  <script>
  function leancloudSelector(url) {
    return document.getElementById(url).querySelector('.leancloud-visitors-count');
  }
  if (CONFIG.page.isPost) {
    function addCount(Counter) {
      var visitors = document.querySelector('.leancloud_visitors');
      var url = visitors.getAttribute('id').trim();
      var title = visitors.getAttribute('data-flag-title').trim();

      Counter('get', `/classes/Counter?where=${JSON.stringify({ url })}`)
        .then(response => response.json())
        .then(({ results }) => {
          if (results.length > 0) {
            var counter = results[0];
            Counter('put', '/classes/Counter/' + counter.objectId, { time: { '__op': 'Increment', 'amount': 1 } })
              .then(response => response.json())
              .then(() => {
                leancloudSelector(url).innerText = counter.time + 1;
              })
              .catch(error => {
                console.log('Failed to save visitor count', error);
              })
          } else {
              leancloudSelector(url).innerText = 'Counter not initialized! More info at console err msg.';
              console.error('ATTENTION! LeanCloud counter has security bug, see how to solve it here: https://github.com/theme-next/hexo-leancloud-counter-security. \n However, you can still use LeanCloud without security, by setting `security` option to `false`.');
            
          }
        })
        .catch(error => {
          console.log('LeanCloud Counter Error', error);
        });
    }
  } else {
    function showTime(Counter) {
      var visitors = document.querySelectorAll('.leancloud_visitors');
      var entries = [...visitors].map(element => {
        return element.getAttribute('id').trim();
      });

      Counter('get', `/classes/Counter?where=${JSON.stringify({ url: { '$in': entries } })}`)
        .then(response => response.json())
        .then(({ results }) => {
          if (results.length === 0) {
            document.querySelectorAll('.leancloud_visitors .leancloud-visitors-count').forEach(element => {
              element.innerText = 0;
            });
            return;
          }
          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.url;
            var time = item.time;
            leancloudSelector(url).innerText = time;
          }
          for (var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = leancloudSelector(url);
            if (element.innerText == '') {
              element.innerText = 0;
            }
          }
        })
        .catch(error => {
          console.log('LeanCloud Counter Error', error);
        });
    }
  }

  fetch('https://app-router.leancloud.cn/2/route?appId=F1yOARDU4POU2Iuir3vgWVrB-gzGzoHsz')
    .then(response => response.json())
    .then(({ api_server }) => {
      var Counter = (method, url, data) => {
        return fetch(`https://${api_server}/1.1${url}`, {
          method: method,
          headers: {
            'X-LC-Id': 'F1yOARDU4POU2Iuir3vgWVrB-gzGzoHsz',
            'X-LC-Key': 'ADyvs9fVUxLnHglgfNMBDNlr',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(data)
        });
      };
      if (CONFIG.page.isPost) {
        const localhost = /http:\/\/(localhost|127.0.0.1|0.0.0.0)/;
        if (localhost.test(document.URL)) return;
        addCount(Counter);
      } else if (document.querySelectorAll('.post-title-link').length >= 1) {
        showTime(Counter);
      }
    });
  </script>






        
      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>



  
















  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/assets/miku.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":0.7},"log":false});</script></body>
</html>
